/***************************************************************************
*   Group Members: Carl Huskey, Dawne Flanagan
*
**/

import java.io.*;
import java.net.InetAddress;

class Client
{
    public static void main(String[] args) throws Exception
    {
        // declare variables
    	String ipAddr = "localHost";
    	InetAddress myIP = InetAddress.getLocalHost();
        int port = 3456; 
        int mySocketPort = 0;
        String sentence = "";
        MyClientSocket mySocket;
        byte bytes[];

        // setup buffer to read form InFromUser
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

        // display client title
        System.out.println("Client");
        System.out.println("======" + "\n");


	    mySocket = new MyClientSocket(ipAddr, port);
	    mySocketPort = mySocket.getSocketPort();

        
        int jobNumber = 0;
        while(jobNumber == 0){
	        System.out.println("Please enter a number for the string operation you wish to perform:\n" +
	        		"1 \tRemove all vowels\n2 \tRemove all spaces\n3 \tCapitalize all\n");
	        
	        sentence = inFromUser.readLine();
	        jobNumber = Integer.parseInt(sentence);
	        
	        if(jobNumber == 1 || jobNumber == 2 || jobNumber == 3){
	        	 break;
	        }else{
	        	jobNumber = 0;
	        }
        }
        
        
        bytes = sentence.getBytes();
        
        mySocket.send(bytes);
        
        System.out.println("Type a string to process.\n" + 
        		"To quit, type CTRL+Z <ENTER> (Windows) or CTRL+D (Linux) on its own line");
        		while((sentence = inFromUser.readLine()) != null)
        		{
        			//Send the message to the server
        			String toSend = ipAddr + ":" + myIP.toString() + ":" + mySocketPort + "*" + sentence +  "*" + jobNumber;
        			bytes = toSend.getBytes();
	        		mySocket.send(bytes);
	        		
	        		//Get back the first msg (ack)
	        		bytes = mySocket.receive();
	        		sentence = bytes.toString(); 
	        		while(sentence != "ack"){
	        			bytes = mySocket.receive();
		        		sentence = bytes.toString();
	        		}
	        		
	        		// Then receive the message and print
	        		bytes = mySocket.receive();
	        		sentence = bytes.toString();
	        		System.out.println(sentence);
        		}
        		mySocket.close(); // since user is done processing strings now.
    }
    
}
